home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Topik / Topik - Disk 16 - KnowAboutIt (19xx)(Topik Public Domain)(PD)[WB].zip / Topik - Disk 16 - KnowAboutIt (19xx)(Topik Public Domain)(PD)[WB].adf / MicroRayDbw / rkm.c < prev    next >
Text File  |  1988-12-11  |  4KB  |  133 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1988, David B. Wecker            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  * This file is part of DBW_uRAY                    *
  7.  *                                    *
  8.  * DBW_uRAY is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts        *
  10.  * responsibility to anyone for the consequences of using it or for    *
  11.  * whether it serves any particular purpose or works at all, unless    *
  12.  * he says so in writing. Refer to the DBW_uRAY General Public        *
  13.  * License for full details.                        *
  14.  *                                    *
  15.  * Everyone is granted permission to copy, modify and redistribute    *
  16.  * DBW_uRAY, but only under the conditions described in the        *
  17.  * DBW_uRAY General Public License. A copy of this license is        *
  18.  * supposed to have been given to you along with DBW_uRAY so you    *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this    *
  21.  * notice must be preserved on all copies.                *
  22.  ************************************************************************
  23.  *                                    *
  24.  * Authors:                                *
  25.  *    DBW - David B. Wecker                        *
  26.  *                                    *
  27.  * Versions:                                *
  28.  *    V1.0 881023 DBW    - First released version            *
  29.  *    V1.1 881110 DBW - Fixed scan coherence code            *
  30.  *    V1.2 881125 DBW - Removed ALL scan coherence code (useless)    *
  31.  *              added "fat" extent boxes            *
  32.  *                                    *
  33.  ************************************************************************/
  34.  
  35. /***********************************************************************/
  36. /************************ run length encoding from RKM *****************/
  37. /***********************************************************************/
  38.  
  39. #define DUMP        0
  40. #define RUN        1
  41. #define MinRun        3
  42. #define MaxRun        128
  43. #define    MaxDat        128
  44. #define GetByte()    (*source++)
  45. #define PutByte(c)    { *dest++ = (c); ++PutSize; }
  46. #define OutDump(nn)    dest = PutDump(dest,nn)
  47. #define OutRun(nn,cc)    dest = PutRun(dest,nn,cc)
  48.  
  49. short    PutSize;
  50. char    buf[256];
  51.  
  52. /* put out nn bytes into the dest buffer */
  53. unsigned char *PutDump(dest,nn)
  54. unsigned char    *dest;
  55. short        nn;
  56.     {
  57.     short i;
  58.  
  59.     PutByte(nn-1);
  60.     for (i = 0; i < nn; i++) PutByte(buf[i]);
  61.     return(dest);
  62.     }
  63.  
  64. /* put out the cc byte nn times into the dest buffer */
  65. unsigned char *PutRun(dest,nn,cc)
  66. unsigned char    *dest;
  67. short        nn,cc;
  68.     {
  69.     PutByte(-(nn-1));
  70.     PutByte(cc);
  71.     return(dest);
  72.     }
  73.  
  74. /* put out a row of the current image */
  75. short PackRow(source,dest,RowSize)
  76. unsigned char    *source,*dest;
  77. short        RowSize;
  78.     {
  79.     char        c,
  80.             lastc = '\000';
  81.     short        mode = DUMP,
  82.             nbuf = 0,        /* number of chars in buf */
  83.             rstart = 0;        /* buf index current run starts */
  84.  
  85.     PutSize = 0;
  86.     buf[0]  = lastc = c = GetByte();
  87.     nbuf    = 1;
  88.     RowSize--;
  89.  
  90.     for (; RowSize; --RowSize) {
  91.     buf[nbuf++] = c = GetByte();
  92.  
  93.     switch (mode) {
  94.         case DUMP:
  95.         if (nbuf > MaxDat) {
  96.         OutDump(nbuf-1);
  97.         buf[0]    = c;
  98.         nbuf    = 1;
  99.         rstart    = 0;
  100.         break;
  101.         }
  102.         if (c == lastc) {
  103.         if (nbuf-rstart >= MinRun) {
  104.             if (rstart > 0) OutDump(rstart);
  105.             mode = RUN;
  106.             }
  107.         else if (rstart == 0) mode = RUN;
  108.         }
  109.         else rstart = nbuf - 1;
  110.         break;
  111.  
  112.         case RUN:
  113.         if ((c != lastc) || (nbuf-rstart > MaxRun)) {
  114.         OutRun((nbuf-1)-rstart,lastc);
  115.         buf[0]    = c;
  116.         nbuf    = 1;
  117.         rstart    = 0;
  118.         mode    = DUMP;
  119.         }
  120.         break;
  121.         }
  122.     lastc = c;
  123.     }
  124.     switch (mode) {
  125.     case DUMP: OutDump(nbuf); break;
  126.     case RUN:  OutRun(nbuf-rstart,lastc); break;
  127.     }
  128.  
  129.     return(PutSize);
  130.     }
  131.  
  132.  
  133.